//
//  ZFResponseModel.m
//  
//
//  Created by a on 2018/11/28.
//  Copyright © 2017年 a. All rights reserved.
//

#import "ResponseModel.h"
#import "JSON.h"

@implementation ResponseModel

- (id)initWithResponseObject:(id)responseObject {
    return [self initWithResponseObject:responseObject url:nil];
}

- (id)initWithResponseObject:(id)responseObject url:(NSString *)url {
    self = [super init];
    if (self) {
        self.url = url;
        
        [self setModelValue:responseObject];
        
        //        if ([responseObject isKindOfClass:[NSData class]]) {
        //            NSMutableDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        //            if ([dictionary isKindOfClass:[NSDictionary class]]) {
        //                [self setModelValue:dictionary];
        //            }
        //            else {
        //                NSLog(@"服务器返回数据json解析失败！");
        //                NSLog(@"%@", dictionary);
        //            }
        //        }
        //        else if ([responseObject isKindOfClass:[NSDictionary class]]) {
        //            [self setModelValue:responseObject];
        //        }
        //        else {
        //            NSLog(@"服务器返回数据格式错误！");
        //            NSLog(@"%@", responseObject);
        //        }
    }
    
    return self;
}

- (void)setModelValue:(id)data {
    
    //    self.code = [data ];
    //    self.isAllowed = [[dictionary notNullObjectForKey:@"isAllowed"] integerValue];
    //    self.message = [dictionary notNullObjectForKey:@"msg"];
    //    self.currentPage = [[dictionary notNullObjectForKey:@"page"] integerValue];
    //    self.totalPage = [[dictionary notNullObjectForKey:@"totalPage"] integerValue];
    //    self.totalResult = [[dictionary notNullObjectForKey:@"totalResult"] integerValue];
    //    self.data = [dictionary notNullObjectForKey:@"data"];
    
    if ([data isKindOfClass:[NSDictionary class]]) {
        NSString *code = [NSString stringWithFormat:@"%@",data[@"status"]];
        self.code = code.integerValue;
        self.message = [NSString stringWithFormat:@"%@",data[@"msg"]];
        DLog(@"message===%@",[self.message class]);
        if (self.message == nil  || self.message.length < 1 || !data[@"msg"]) {
            self.message = [NSString stringWithFormat:@"%@",data[@"message"]];
        }
        self.currentPage = [data[@"pageable"][@"pageNumber"] integerValue];
        self.totalPage = [data[@"totalPages"] integerValue];
        
        NSMutableDictionary *finallData = [[NSMutableDictionary alloc] initWithDictionary:data];
        NSDictionary *newDict = [self changeType:finallData];
        self.data = newDict;
        //        }
        
        
        
        //        }
        
    } if ([data isKindOfClass:[NSArray class]]) {
        self.code = 200;
        self.message = @"获取成功";
        self.currentPage = 0;
        self.totalPage = 0;
        
        NSMutableArray *finallData = [[NSMutableArray alloc] initWithArray:data];
        NSArray *newDict = [self changeType:finallData];
        self.data = newDict;
    }else {
        NSDictionary *newDict = [self changeType:data];
        self.data = newDict;
    }
    
    NSLog(@"%@", self);
    if (self.code == -10011) {
        
    }
    
}

//将NSDictionary中的Null类型的项目转化成@""
-(NSDictionary *)nullDic:(NSDictionary *)myDic
{
    NSArray *keyArr = [myDic allKeys];
    NSMutableDictionary *resDic = [[NSMutableDictionary alloc]init];
    for (int i = 0; i < keyArr.count; i ++)
    {
        id obj = [myDic objectForKey:keyArr[i]];
        
        obj = [self changeType:obj];
        
        [resDic setObject:obj forKey:keyArr[i]];
    }
    return resDic;
}

//将NSArray中的Null类型的项目转化成@""
-(NSArray *)nullArr:(NSArray *)myArr
{
    NSMutableArray *resArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < myArr.count; i ++)
    {
        id obj = myArr[i];
        
        obj = [self changeType:obj];
        
        [resArr addObject:obj];
    }
    return resArr;
}

//将NSString类型的原路返回
-(NSString *)stringToString:(NSString *)string
{
    return string;
}

//将Null类型的项目转化成@""
-(NSString *)nullToString
{
    return @"";
}

//主要方法
//类型识别:将所有的NSNull类型转化成@""
-(id)changeType:(id)myObj
{
    if ([myObj isKindOfClass:[NSDictionary class]])
    {
        return [self nullDic:myObj];
    }
    else if([myObj isKindOfClass:[NSArray class]])
    {
        return [self nullArr:myObj];
    }
    else if([myObj isKindOfClass:[NSString class]])
    {
        return [self stringToString:myObj];
    }
    else if([myObj isKindOfClass:[NSNull class]])
    {
        return [self nullToString];
    }
    else
    {
        return myObj;
    }
}


-(id)parseJSONStringToNSDictionary:(NSString *)JSONString {
    
    NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization
                     JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments error:&error];
    if (jsonObject != nil && error == nil){
        NSLog(@"反序列化成功...");
        if ([jsonObject isKindOfClass:[NSDictionary class]]){
            NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
            //            NSLog(@"反序列化后的dictionary数据 = %@", deserializedDictionary);
            return deserializedDictionary;
        }else if ([jsonObject isKindOfClass:[NSArray class]]){
            NSArray *deserializedArray = (NSArray *)jsonObject;
            NSLog(@"反序列化json后的数组 = %@", deserializedArray);
            return deserializedArray;
        }else {
            NSString *str = [NSString stringWithFormat:@"%@",jsonObject];
            return str;
        }
    }else{
        NSLog(@"反序列化时发生一个错误");
        return nil;
    }
    
}

- (NSString *)description {
    NSMutableString *string = [NSMutableString string];
//    [string appendString:@"\nResponse Data:"];
//    if (self.url) {
//        [string appendFormat:@"\n  url:%@", self.url];
//    }
//    [string appendFormat:@"\n  code:%ld", (long)self.code];
//    [string appendFormat:@"\n  isAllowed:%ld", (long)self.isAllowed];
//    [string appendFormat:@"\n  totalPage:%ld", (long)self.totalPage];
//    [string appendFormat:@"\n  currentPage:%ld", (long)self.currentPage];
//    [string appendFormat:@"\n  totalResult:%ld", (long)self.totalResult];
//    [string appendFormat:@"\n  message:%@", self.message];
//    [string appendFormat:@"\n  data:%@\n", self.data];
//    [string appendString:@"*********************************************************************"];
    return string;
}

@end
